import { defineStore } from 'pinia'
import { Utils } from '@/common/utils.js'
import type { 
	SystemInfoType,
	UserInfoType,
	PosiListType,
	SetPosiListType
 } from './mainType.ts'

export const useCommonStore = defineStore('COMMON', () => {
	const menuData = {
		"menuOptions": [
			{
				"id": "serviceManage",
				"name": "服务管理",
				"link": "serviceManage",
				"icon": "menu"
			}, {
				"id": "accessManage",
				"name": "**管理",
				"link": "accessManage",
				"icon": "Connection",
				"arr": [
					{
						"id": "systemManage",
						"name": "**管理",
						"link": "systemManage",
						"icon": "link"
					}, {
						"id": "authorizeCodeManage",
						"name": "**管理",
						"link": "authorizeCodeManage",
						"icon": "link"
					}
				]
			}, {
				"id": "configurationManage",
				"name": "配置管理",
				"link": "configurationManage",
				"icon": "HelpFilled",
				"arr": [
					{
						"id": "configurationExtend",
						"name": "扩展配置",
						"link": "configurationExtend",
						"icon": "link"
					}, {
						"id": "flowManage",
						"name": "流控管理",
						"link": "flowManage",
						"icon": "link",
						"arr": [
							{
								"id": "flowAllConfiguration",
								"name": "流控全局配置",
								"link": "flowAllConfiguration",
								"icon": "link"
							}, {
								"id": "flowStrategyConfiguration",
								"name": "流控策略配置",
								"link": "flowStrategyConfiguration",
								"icon": "link"
							}
						]
					}, {
						"id": "BWNameListManage",
						"name": "**名单管理",
						"link": "BWNameListManage",
						"icon": "link",
						"arr": [
							{
								"id": "BWNameListAllConfiguration",
								"name": "**全局配置",
								"link": "BWNameListAllConfiguration",
								"icon": "link"
							}, {
								"id": "BWNameListStrategyConfiguration",
								"name": "**策略配置",
								"link": "BWNameListStrategyConfiguration",
								"icon": "link"
							}
						]
					}
				]
			}, {
				"id": "offerParametersManage",
				"name": "**参数",
				"link": "offerParametersManage",
				"icon": "TrendCharts",
				"arr": [
					{
						"id": "staticParamerts",
						"name": "**基本信息",
						"link": "staticParamerts",
						"icon": "Link"
					}, {
						"id": "dynamicParamerts",
						"name": "**运行信息",
						"link": "dynamicParamerts",
						"icon": "Link"
					}
				]
			}, {
				"id": "bandSecuParametersManage",
				"name": "**管理",
				"link": "bandSecuParametersManage",
				"icon": "Histogram",
				"arr": [
					{
						"id": "bandSecuSysTemInfo",
						"name": "**系统信息",
						"link": "bandSecuSysTemInfo",
						"icon": "Link"
					}
				]
			}, {
				"id": "etcd",
				"name": "**数据浏览",
				"link": "etcd",
				"icon": "Grid"
			}, {
				"id": "command",
				"name": "**实时指令",
				"link": "command",
				"icon": "Promotion"
			}, {
				"id": "subscribeManage",
				"name": "**查询服务",
				"link": "subscribeManage",
				"icon": "ZoomIn"
			}, {
				"id": "userManage",
				"name": "用户管理",
				"link": "userManage",
				"icon": "UserFilled"
			}, {
				"id": "operationRecords",
				"name": "**记录",
				"link": "operationRecords",
				"icon": "DocumentChecked"
			}, {
				"id": "faq",
				"name": "**帮助",
				"link": "faq",
				"icon": "questionFilled"
			}
		],
		"menuHideConfiguration": {
			"tap": ["subscribeManage", "offerParametersManage", "bandSecuParametersManage"],
			"tpp": ["offerParametersManage", "bandSecuParametersManage"],
			"tac": ["flowManage", "BWNameListManage", "subscribeManage", "accessManage"],
			"_all_": []
		}
	}
	const userList = reactive([
		{
			userName: 'admin',
			password: 'aaa111',
			userType: 999999,
			is_need_modify_passwd: false
		}, {
			userName: '游客',
			password: 'aaa111',
			userType: 2,
			is_need_modify_passwd: false
		}
	])
    const systemInfo = reactive({
        systemId: '',
        systemNameCN: '',
        systemFullname: ''
    });
    const setSystemInfo = (systemInfoConfig: SystemInfoType) => Object.assign(systemInfo, systemInfoConfig);
    const token = ref('');
    const setToken = (value: string) => token.value = value;
    const userStatus = ref(false);
    const setUserStatus = (value: boolean) => userStatus.value = value;
    const userInfo = reactive({
        userName: '游客',
        userType: -1,
        ttl: 0,
        beginTime: 0
    });
    const setUserInfo = (userInfoConfig: UserInfoType) => Object.assign(userInfo, userInfoConfig);
    const posiList = reactive<PosiListType>({
        arr: []
    });
    const setPosiList = (setPosiListConfig: SetPosiListType) => posiList.arr = setPosiListConfig;
    return {
		menuData,
        systemInfo,
        setSystemInfo,
        token,
        setToken,
		userList,
        userStatus,
        setUserStatus,
        userInfo,
        setUserInfo,
        posiList,
        setPosiList
    };
}, {
    persist: {
        enabled: true,
        strategies: [{
            storage: sessionStorage,
            paths: ['posiList']
        }, {
            storage: localStorage,
            paths: ['menuData', 'systemInfo', 'token', 'userList', 'userStatus', 'userInfo']
        }]
    }
})

export const useCommonApiStore = defineStore('API', () => {
	// 系统服务
	type ServiceListType = {
		id: number
		name: string
		version?: string
		build_info: string
		deploy_type: number
		is_primary:boolean
		weight: number
		is_online: boolean
		is_enable: boolean
		listen_addr?: string
		nat_addr?: string
		listen_addr_admin?: string
		start_timeStamp?: string
		[property: string]: any
	}
	type MainExpandConfType = {
		key: string
		value: string | number | boolean
		[property: string]: any
	}
	type GrayExpandConfType = {
		instance_id: string
		key: string
		value: string | number | boolean
		[property: string]: any
	}
	type ServiceInfoType = {
		service_name: string
		deploy_type: number
		sys_id?: number
		serviceList?: ServiceListType[]
		mainExpandConf?: MainExpandConfType[]
		grayExpandConf?: GrayExpandConfType[]
		[property: string]: any
	}
	type SystemServiceInfoType = {
		tap: ServiceInfoType[]
	}
	const systemServiceInfo: SystemServiceInfoType = reactive({
		tap: [// 服务列表
			{
				sys_id: 1,
				service_name: 'tap_service_A',
				// 1: success, 2: warning, 3: danger
				deploy_type: 1,
				serviceList: [// 服务实例
					{
						id: 1,
						name: 'insatancd1',
						version: '1.0.0',
						build_info: '1.0.1',
						deploy_type: 1,
						is_primary: true,
						weight: 1,
						is_online: false,
						is_enable: false,
						listen_addr: '127.0.0.1:9090',
						nat_addr: '127.255.255.255:8080',
						listen_addr_admin: '127.0.0.1:8080',
						start_timeStamp: '2023-07-11 08:30:00'
					}, {
						id: 2,
						name: 'insatancd1',
						version: '1.0.0',
						build_info: '1.0.1',
						deploy_type: 1,
						is_primary: true,
						weight: 0,
						is_online: false,
						is_enable: false,
						listen_addr: '127.0.0.1:9090',
						nat_addr: '127.255.255.255:8080',
						listen_addr_admin: '127.0.0.1:8080',
						start_timeStamp: '2023-07-11 08:30:00'
					}
				],
				mainExpandConf: [
					{
						"key": "LsogConf",
						"value": 123
					}, {
						"key": "BlogConf",
						"value": "\"hello world\""
					}, {
						"key": "JogConf",
						"value": "{\"Level\": \"JogConfInfo\", \"Path\": \"./log/\"}"
					}
				],
				grayExpandConf: [
					{
						"instance_id": "portal_access(127.0.0.0:8080)",
						"key": "LogConf",
						"value": "{\"Level\": \"Debug\", \"Path\": \"./log/\"}"
					}, {
						"instance_id": "portal_access(127.0.0.1:9090)",
						"key": "LogConfTest_xxx",
						"value": "{\"Level\": \"Debug\", \"Path\": \"./log/\"}"
					}, {
						"instance_id": "portal_access(127.0.0.1:9090)",
						"key": "LogConfTest_aaa",
						"value": 1
					}, {
						"instance_id": "portal_access(127.0.0.1:9090)",
						"key": "LogConfTest_bbb",
						"value": 2
					}
				],
				flowCtrInof: [// 流控配置
					{
						// 流控开关
						is_enable: false,
						list: [{
							'sys_id': 0,
							'id': 0,
							'alarm_rate': 99,
							'desc': '',
							'filters': [
								{
									'k': 'ip_group',
									'v': 'aa'
								}, {
									'k': 'fund_account',
									'v': 'haoduoyu'
								}, {
									'k': 'api_group',
									'v': 'hello'
								}
							],
							'filters_orig': '',
							'is_enable':false,
							'priority': '',
							'ctime':'2024-04-22 20:07:58',
							'rate': '',
							'utime':'2024-05-23 21:15:12'
						}]
					}
				],
				apiInfo: [// API分组
					{
						group_name: '',
						desc: ''
					}
				],
				ipInfo: [// IP分组
					{
						group_name: '',
						desc: ''
					}
				]
			}, {
				sys_id: 2,
				service_name: 'tap_service_B',
				// 1: success, 2: warning, 2: danger
				deploy_type: 1,
				serviceList: [// 服务实例
					{
						id: 1,
						name: 'insatancd1',
						version: '1.0.0',
						build_info: '1.0.1',
						deploy_type: 1,
						is_primary: true,
						weight: 1,
						is_online: false,
						is_enable: false,
						listen_addr: '127.0.0.1:9090',
						nat_addr: '127.255.255.255:8080',
						listen_addr_admin: '127.0.0.1:8080',
						start_timeStamp: '2023-07-11 08:30:00'
					}
				],
				mainExpandConf: [
					{
						"key": "LsogConf",
						"value": 123
					}
				],
				grayExpandConf: [
					{
						"instance_id": "portal_access(127.0.0.0:8080)",
						"key": "LogConf",
						"value": "{\"Level\": \"Debug\", \"Path\": \"./log/\"}"
					}
				],
				flowCtrInof: [// 流控配置
					{
						// 流控开关
						is_enable: false,
						list: [{
							'sys_id': 0,
							'id': 0,
							'alarm_rate': 99,
							'desc': '',
							'filters': [
								{
									'k': 'ip_group',
									'v': 'aa'
								}, {
									'k': 'fund_account',
									'v': 'haoduoyu'
								}, {
									'k': 'api_group',
									'v': 'hello'
								}
							],
							'filters_orig': '',
							'is_enable':false,
							'priority': '',
							'ctime':'2024-04-22 20:07:58',
							'rate': '',
							'utime':'2024-05-23 21:15:12'
						}]
					}
				],
				apiInfo: [// API分组
					{
						group_name: '',
						desc: ''
					}
				],
				ipInfo: [// IP分组
					{
						group_name: '',
						desc: ''
					}
				]
			}, {
				sys_id: 3,
				service_name: 'tap_service_C',
				deploy_type: 2,
				serviceList: [// 服务实例
					{
						id: 2,
						name: 'insatancd1',
						version: '1.0.0',
						build_info: '1.0.2',
						deploy_type: 2,
						is_primary: false,
						weight: 0,
						is_online: true,
						is_enable: false,
						listen_addr: '127.0.0.1:9090',
						nat_addr: '127.255.255.255:8080',
						listen_addr_admin: '127.0.0.1:8080',
						start_timeStamp: '2023-07-11 08:30:00'
					}
				],
				mainExpandConf: [
					{
						"key": "LsogConf",
						"value": "123"
					}, {
						"key": "LsogConf",
						"value": "123"
					}, {
						"key": "LsogConf",
						"value": "123"
					}, {
						"key": "LsogConf",
						"value": "123"
					}, {
						"key": "LsogConf",
						"value": "123"
					}
				],
				grayExpandConf: [
					{
						"instance_id": "portal_access(127.0.0.0:8080)",
						"key": "LogConf",
						"value": "{\"Level\": \"Debug\", \"Path\": \"./log/\"}"
					}, {
						"instance_id": "portal_access(127.0.0.0:8080)",
						"key": "LogConf",
						"value": "{\"Level\": \"Debug\", \"Path\": \"./log/\"}"
					}, {
						"instance_id": "portal_access(127.0.0.0:8080)",
						"key": "LogConf",
						"value": "{\"Level\": \"Debug\", \"Path\": \"./log/\"}"
					}
				],
				flowCtrInof: [// 流控配置
					{
						// 流控开关
						is_enable: false,
						list: [{
							'sys_id': 0,
							'id': 0,
							'alarm_rate': 99,
							'desc': '',
							'filters': [
								{
									'k': 'ip_group',
									'v': 'aa'
								}, {
									'k': 'fund_account',
									'v': 'haoduoyu'
								}, {
									'k': 'api_group',
									'v': 'hello'
								}
							],
							'filters_orig': '',
							'is_enable':false,
							'priority': '',
							'ctime':'2024-04-22 20:07:58',
							'rate': '',
							'utime':'2024-05-23 21:15:12'
						}]
					}
				],
				apiInfo: [// API分组
					{
						group_name: '',
						desc: ''
					}
				],
				ipInfo: [// IP分组
					{
						group_name: '',
						desc: ''
					}
				]
			}, {
				sys_id: 4,
				service_name: 'tap_service_D',
				// 0: success, 1: warning, 2: danger
				deploy_type: 3,
				serviceList: [// 服务实例
					{
						id: 3,
						name: 'insatancd1',
						version: '1.0.0',
						build_info: '1.0.3',
						deploy_type: 3,
						is_primary: false,
						weight: 0,
						is_online: true,
						is_enable: false,
						listen_addr: '127.0.0.1:9090',
						nat_addr: '127.255.255.255:8080',
						listen_addr_admin: '127.0.0.1:8080',
						start_timeStamp: '2023-07-11 08:30:00'
					}
				],
				mainExpandConf: [
					{
						"key": "LogConf",
						"value": "123"
					}, {
						"key": "DepConf",
						"value": "123"
					}
				],
				grayExpandConf: [
					{
						"instance_id": "portal_access(127.0.0.0:8080)",
						"key": "LogConf",
						"value": "{\"Level\": \"Debug\", \"Path\": \"./log/\"}"
					}, {
						"instance_id": "portal_access(127.0.0.0:9090)",
						"key": "DepConf",
						"value": "{\"Level\": \"Debug\", \"Path\": \"./log/\"}"
					}
				],
				flowCtrInof: [// 流控配置
					{
						// 流控开关
						is_enable: false,
						list: [{
							'sys_id': 0,
							'id': 0,
							'alarm_rate': 99,
							'desc': '',
							'filters': [
								{
									'k': 'ip_group',
									'v': 'aa'
								}, {
									'k': 'fund_account',
									'v': 'haoduoyu'
								}, {
									'k': 'api_group',
									'v': 'hello'
								}
							],
							'filters_orig': '',
							'is_enable':false,
							'priority': '',
							'ctime':'2024-04-22 20:07:58',
							'rate': '',
							'utime':'2024-05-23 21:15:12'
						}]
					}
				],
				apiInfo: [// API分组
					{
						group_name: '',
						desc: ''
					}
				],
				ipInfo: [// IP分组
					{
						group_name: '',
						desc: ''
					}
				]
			}
		],
		// tcp: [{}],
		// tpp: [{}],
		// tmp: [{}]
	})

	// 服务列表
	const getServiceList = ({ sys_name }: { sys_name: string }): ServiceInfoType[] => {
		const serviceList = systemServiceInfo[sys_name as keyof typeof systemServiceInfo]
		return serviceList.map(({ service_name, deploy_type }) => (
			{
				service_name,
				deploy_type
			}
		))
	}
	// 服务实例
	const getServiceListDetail = ({
		sys_name,
		service_name,
		page_no,
		page_size
	} : {
		sys_name: string
		service_name: string
		page_no: number
		page_size: number
	}) => {
		const systemServiceList = systemServiceInfo[sys_name as keyof typeof systemServiceInfo]
		let data = [], total = 0
		for(let item of systemServiceList) {
			if(item.service_name === service_name) {
				const serviceListData = item.serviceList!
				total = serviceListData.length
				const s = page_size * (page_no - 1)
				const e = page_size * page_no > total
					? total - page_size * (page_no - 1)
					: page_size
				for(let i = s; i < s + e; i++) data.push(serviceListData[i])
			}
		}
		return {
			data: Utils.deepCopy(data), 
			total: Utils.deepCopy(total)
		}
	}

	// 配置管理-扩展配置-主版本
	const getServiceDycofigMainList = ({
		sys_name,
		service_name,
		page_no,
		page_size
	} : {
		sys_name: string
		service_name: string
		page_no: number
		page_size: number
	}) => {
		const systemServiceList = systemServiceInfo[sys_name as keyof typeof systemServiceInfo]
		let data = [], total = 0
		for(let item of systemServiceList) {
			if(item.service_name === service_name) {
				const mainExpandConf = item.mainExpandConf!
				total = mainExpandConf.length
				const s = page_size * (page_no - 1)
				const e = page_size * page_no > total
					? total - page_size * (page_no - 1)
					: page_size
				for(let i = s; i < s + e; i++) data.push(mainExpandConf[i])
			}
		}
		return {
			data: Utils.deepCopy(data), 
			total: Utils.deepCopy(total)
		}
	}
	// 配置管理-扩展配置-灰度版本
	const getServiceDycofigGrayList = ({
		sys_name,
		service_name,
		page_no,
		page_size
	} : {
		sys_name: string
		service_name: string
		page_no: number
		page_size: number
	}) => {
		const systemServiceList = systemServiceInfo[sys_name as keyof typeof systemServiceInfo]
		let data = [], total = 0
		for(let item of systemServiceList) {
			if(item.service_name === service_name) {
				const grayExpandConf = item.grayExpandConf!
				total = grayExpandConf.length
				const s = page_size * (page_no - 1)
				const e = page_size * page_no > total
					? total - page_size * (page_no - 1)
					: page_size
				for(let i = s; i < s + e; i++) data.push(grayExpandConf[i])
			}
		}
		return {
			data: Utils.deepCopy(data), 
			total: Utils.deepCopy(total)
		}
	}

	return {
		systemServiceInfo, 
		getServiceList, getServiceListDetail,
		getServiceDycofigMainList, getServiceDycofigGrayList
	}
}, {
	persist: {
		enabled: true,
		strategies: [
			{
				storage: sessionStorage, 
				paths: ['test']
			}, {
				storage: localStorage, 
				paths: [
					'systemServiceInfo',
				]
			}
		]
	}
})
